// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import VueAxios from 'vue-axios'
import ElementUI from 'element-ui'
// import 'element-ui/lib/theme-default/index.css'
import '../element-#018e45/index.css'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import * as Auth from 'common/auth'

axios.defaults.baseURL = '/admin'

// Add a request interceptor
axios.interceptors.request.use(function (config) {
  NProgress.start()
  // TODO add authToken
  return config
}, function (error) {
  NProgress.done()
  // Do something with request error
  console.log(`request error: ${error}`)
  return Promise.reject(error)
})

// Add a response interceptor
axios.interceptors.response.use(function (response) {
  NProgress.done()
  return response
}, function (error) {
  NProgress.done()
  if (error.response.status === 401) {
    Auth.redirectLogin()
  } else {
    window.vue.$message({
      title: '提示',
      message: `${error.response.data.error}`,
      type: 'error'
    })
  }
  return Promise.reject(error)
})

Vue.use(Vuex)
Vue.use(VueAxios, axios)
Vue.use(ElementUI)

import App from './App'
import { router } from './routes'

Vue.prototype.$eventBus = Vue.prototype.$eventBus || new Vue()

/* eslint-disable no-new */
const VueComponent = new Vue({
  el: '#app',
  template: '<App/>',
  router: router,
  components: { App }
})

window.vue = VueComponent
